$(function(){
    //菜单点击
    $(".J_menuItem").on('click',function(){
        var url = $(this).attr('href');
        $("#J_iframe").attr('src',url);
        return false;
    });
 	getJurisdiction();
    getOneAdmin();

});

/**
 * 退出登录
 */
function loginout() {
		$.ajax({
		url: urlcore + "/api/admin/logout",
		type: "GET",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
			window.location.href="login.html";
		} else {
			alert(data.msg);
		}

		},
		error:function() {
			alert("error");
		}
	});
	
}

function getOneAdmin() {
    //获取个人信息
	$.ajax({
		url: urlcore + "/api/admin/getadmin",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				var userName = data.data.userName;
				var nickName=data.data.nickName;
				$('#admin_img_user_name').append(
					        '<img alt="image" class="img-circle" src="img/toxiang.jpeg" style="height: 100%; width: 100%;">'+
                            '<h4><a href="javascript:;"  data-toggle="modal" data-target="#updatePassword">'+nickName+'</a></h4>'
				);
				$('#nick_').text(nickName);
				$('#adminId').val(data.data.id);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else if(data.code == 'PARAMETER_INVALID') {
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			}
			else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});	
}

//修改信息
function toUpdatePassword() {
	var id = $('#adminId').val();
	var oldPassword = $('#oldPassword').val().trim();
	var password = $('#password').val().trim();
	var aginPassword = $('#aginPassword').val().trim();
	var userName = $('#userName').val().trim();
	var nick = $('#nick').val().trim();
	if (password != aginPassword) {
		alert("密码不一致");
		return;
	}
	if (oldPassword=='' && password=='' && aginPassword=='' && userName=='' && nick=='') {
		return;
	}
	$.ajax({
		url: urlcore + "/admin/update/password/old?id="+id+"&password="+password+"&oldPassword="+oldPassword+"&userName="+userName+"&nick="+nick,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				alert("修改成功，请重新登陆！");
				top.window.location.href="login.html";
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}
			}

		},
		error:function() {
			alert("error");
		}
	});	
}


function getJurisdiction(){
	$.ajax({
		url: urlcore + "/api/admin/getJurisdiction",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			var list = data.data;
			$.each(list,function(i,n){
				var da = n.one;
				$("#menu_"+da.id).attr("href",da.catalogUrl);
				$("#menu_"+da.id).css("display","block");
				if(n.two.length>0){
					$.each(n.two, function(j,m) {	
						$("#menu_"+m.id).attr("href",m.catalogUrl);
					});
				}	
			});
		},error:function() {
			alert("error");
		}
	});			
}